package com.yuntsg.nnsfcp.config;

import com.yuntsg.nnsfcp.front.entity.BackUpDetail;
import com.yuntsg.nnsfcp.front.mapper.BackUpDetailMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContext;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Calendar;

@Component
public class AutomaticBackUpTask {

    @Resource
    BackUpDetailMapper backUpDetailMapper;

    @Resource
    BackUpConfig backUpConfig;

    @Resource
    UploadConfig uploadConfig;


    //    @Scheduled(cron = "0 0 0 * * *") //每天进行判断后没有当天的工单 如果有进行备份
//    @Scheduled(cron = "0 0/1 * * * *") //每1分钟进行一次
    public void testTask() throws IOException {
        //每年每月一号提示人员进行备份
        System.out.println("每天测试");

//        String addTime = DateUtil.backOneDay_ymd();
//        List<OrderBaseInfo> orderBaseInfoList = orderBaseInfoMapper.selectList(new QueryWrapper<OrderBaseInfo>().eq("addtime", addTime));
//        if (orderBaseInfoList.size() >= 1) {
//            /*
//             * 凌晨备份 备份昨天一天的数据 如果有订单的话进行备份
//             * */
//            String serverPath = request.getServletContext().getRealPath(uploadConfig.getUploadPath() + addTime + "/");
//            File targetFile = new File(serverPath);
//            if (targetFile.exists()) {
//                //如果文件夹存在就进行压缩
//
//            }

        String backconfigPath = backUpConfig.getPath();
        String UpconfigPath = uploadConfig.getUploadPath();
//        String upckpath = request.getServletContext().getRealPath(UpconfigPath + "/");
        String upckpath = getPath() + UpconfigPath;
//        String backpath = request.getServletContext().getRealPath(backconfigPath + "/");
        String backpath = getPath() + backconfigPath;
        Calendar calendar = Calendar.getInstance();
        String date = "" + calendar.get(Calendar.YEAR) + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DATE);
        File targetFile = new File(backpath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        Runtime.getRuntime().exec("tar -zcvf " + backpath + date + ".tar.gz" + " " + upckpath);

        /*
         * 备份mysql
         *
         * */
        String userName = backUpConfig.getUsername();
        String pass = backUpConfig.getPassword();
        String dbName = backUpConfig.getDatabasename();
        System.out.println(backpath);
        String[] cmd = new String[]{"/bin/sh", "-c", "/usr/bin/mysqldump -u" + userName + " " + "-p" + pass + " " + dbName + " " + ">" + backpath + date + ".sql"};
        Runtime.getRuntime().exec(cmd);
//        BackUpDetail backUpDetail = backUpDetailMapper.selectOne(new QueryWrapper<BackUpDetail>().eq("status", 0));
        BackUpDetail backUpDetail = new BackUpDetail();
        backUpDetail.setUid(1001);
        backUpDetail.setUsername("系统超级管理员");
        backUpDetail.setDbPath(backconfigPath + date + ".sql");
        backUpDetail.setFilePath(backconfigPath + date + ".tar.gz");
        backUpDetail.setBackTime(LocalDateTime.now());
        backUpDetail.setStatus(1);
        backUpDetailMapper.insert(backUpDetail);

        /*
         * 生成数据进行提示 每次后台人员登录后如果有未进行备份提示备份 记录下来备份人员
         *
         * */
//        BackUpDetail backUpDetail = new BackUpDetail();
//        backUpDetail.setBackTime(LocalDateTime.now());
//        backUpDetail.setStatus(0); //未备份
//        backUpDetailMapper.insert(backUpDetail);

    }


    /*
     * 2019年6月24日17:06:51
     * 若是每月第一天到了晚上十一点还没有备份 采用程序自动备份
     *
     * */
//    @Scheduled(cron = "0 0 23 1 * *") //每月月初提示进行备份
//    public void machinetestTask() throws IOException {
//        System.out.println("程序是否自动备份");
//        /*
//         * 服务器路径测试
//         *
//         * */
//        String backconfigPath = backUpConfig.getPath();
//        String UpconfigPath = uploadConfig.getUploadPath();
//        String upckpath = request.getServletContext().getRealPath(UpconfigPath + "/");
//        String backpath = request.getServletContext().getRealPath(backconfigPath + "/");
//        Calendar calendar = Calendar.getInstance();
//        String date = "" + calendar.get(Calendar.YEAR) + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DATE);
//        File targetFile = new File(backpath);
//        if (!targetFile.exists()) {
//            targetFile.mkdirs();
//        }
//        Runtime.getRuntime().exec("tar -zcvf " + backpath + date + ".tar.gz" + " " + upckpath);
//
//        /*
//         * 备份mysql
//         *
//         * */
//        String userName = backUpConfig.getUsername();
//        String pass = backUpConfig.getPassword();
//        String dbName = backUpConfig.getDatabasename();
//        System.out.println(backpath);
//        String[] cmd = new String[]{"/bin/sh", "-c", "/usr/bin/mysqldump -u" + userName + " " + "-p" + pass + " " + dbName + " " + ">" + backpath + date + ".sql"};
//        Runtime.getRuntime().exec(cmd);
//        BackUpDetail backUpDetail = backUpDetailMapper.selectOne(new QueryWrapper<BackUpDetail>().eq("status", 0));
//        backUpDetail.setUid(1001);
//        backUpDetail.setUsername("系统超级管理员");
//        backUpDetail.setDbPath(backpath + date + ".sql");
//        backUpDetail.setFilePath(backpath + date + ".tar.gz");
//        backUpDetail.setBackTime(LocalDateTime.now());
//        backUpDetail.setStatus(1);
//        backUpDetailMapper.updateById(backUpDetail);
//    }


    private String getPath() {
        //服务器: tomcat/webapps/root
        //windows: 工作路径/项目名
        try {
            String root = RequestContext.class.getResource("/").getFile();
            return new File(root).getParentFile().getParentFile().getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
